import Vue from 'vue'
const Vconsole = require('@/utils/vconsole.min.js')
let BASE_URL;
if(process.env.NODE_ENV == 'production'){
  if(location.pathname == '/activity-pages/tttt/luckylottery/pages/'){
    const con = new Vconsole()
    BASE_URL = 'https://st.gd10010.cn/relycube/tttt/luckylottery'
  }else{
    BASE_URL = 'https://st.gd10010.cn/relycube/luckylottery'   //   正式
  }
}else{
  BASE_URL = ''
}
export {BASE_URL}


export const get = async (url, params, load, config = {}) =>{
  return await new Promise((resolve,reject)=>{
    uni.request({
      url: BASE_URL + url,
      data: params,
      timeout: 80000,
      method: 'GET',
      header: {
        ...config
      },
      success: res => {
        console.log('请求成功',res.data)
        const {code} = res.data
        resolve(res.data)
        /** end 处理结束*/
      },
      fail: error => {
        reject(error)
      },
      complete:res=>{
        if(load) uni.hideLoading();
      }
    })
  })
}


export const post = async (url, params, load, config = {}) =>{
  return await new Promise((resolve,reject)=>{
    uni.request({
      url: BASE_URL + url,
      data: params,
      timeout: 80000,
      method: 'POST',
      header: {
        ...config
      },
      success: res => {
        console.log('请求成功',res.data)
        const {code} = res.data
        if(code == 403){
          uni.showToast({
            title:res.data.msg,
            icon:'none'
          })
        }
        resolve(res.data)
        /** end 处理结束*/
      },
      fail: error => {
        reject(error)
      },
      complete:res=>{
        if(load) uni.hideLoading();
      }
    })
  })
}

//获取当前时间
function formatDateTime() {
  let date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  const milliSecond = date.getMilliseconds();
  return (
    [year, month, day].map(formatNumber).join("/") +
    " " +
    [hour, minute, second].map(formatNumber).join(":") +
    "." +
    milliSecond
  );
}
function formatNumber(n) {
  let nStr;
  nStr = n.toString();
  return nStr[1] ? nStr : "0" + nStr;
}

export const point = (data) => {
    let action_time = formatDateTime(); //获取当前时间
    let postData = {
      version_code: "V2.0.0", // 版本 固定不用改  必须传
      client_type: "mb-h5", // 客户端类型 固定不用改 必须传
      agent_code: "OT7G5XRJ", // 接入编码 固定不用改 必须传
      device_id: "12", // 用户编码 固定不用改 必须传
      action_time,  //必须传
      source_url: "960000000", //对照表填 必须传
      present_url: "960000000",//对照表填 必须传
      ...data
    };
    uni.request({
      url: "https://st.gd10010.cn/relycube/orbit/log",
      method: "post",
      data: JSON.stringify(postData),
      headers: {
        "Content-Type": "application/json",
      },
    })
  }